package helpers;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import models.GeoModel;

import data.GeoData;

public class SessionHelper {
	public static GeoModel getCurrentCity(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Cookie cookies[] = request.getCookies();

		String alias = "";

		if (session.getAttribute("city") != null) {
			alias = (String) session.getAttribute("city");
		} else if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				if (cookies[i].getName().equals("city")) {
					alias = cookies[i].getValue();
					break;
				}
			}
		}

		GeoData geoData = new GeoData();
		GeoModel city = null;

		if (alias != null && !alias.isEmpty()) {
			city = geoData.getGeoByAlias(alias);
		}

		if (city == null) {
			city = geoData.getFirstCity();
		}

		return city;
	}
}